﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;
using System.Collections;

using CE.User;
using CE.Database;
using CE.Global;

public partial class User_reject_users : System.Web.UI.Page
{
    string PageCode = "CXRPTGRURRJUE";
    public bool _CountStatus = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
            }
            ClearFields();
        }
    }

    protected void btnReject_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            trAlredyRejected.Visible = false;
            DataTable _dtResult = new DataTable();
            UserSubscriberDB _usersubscriberdb = new UserSubscriberDB();
            string EmailList = txtEmail.Text.Trim();
            EmailList = EmailList.Replace("\r\n", "");
            _dtResult = _usersubscriberdb.UnSubscribeUserList(EmailList, Convert.ToInt64(ddlRejectType.Text), ddlRejectType.SelectedItem.Text);
            if (_dtResult != null && _dtResult.Rows.Count > 0)
            {
                string Already, Error, SubscribeReject, Reject, Users;
                Already = ""; 
                Error = ""; 
                SubscribeReject = "0"; 
                Reject = "0"; 
                Users = "0";
                if (_dtResult.Rows[0]["Already"] != DBNull.Value && _dtResult.Rows[0]["Already"].ToString() != string.Empty)
                {
                    Already = _dtResult.Rows[0]["Already"].ToString();
                    txtAlredyRejected.Text = Already.ToString().Trim();
                }
                if (_dtResult.Rows[0]["Error"] != DBNull.Value && _dtResult.Rows[0]["Error"].ToString() != string.Empty)
                {
                    Error = _dtResult.Rows[0]["Error"].ToString();
                }
                if (_dtResult.Rows[0]["SubscribeReject"] != DBNull.Value && _dtResult.Rows[0]["SubscribeReject"].ToString() != string.Empty)
                {
                    SubscribeReject = _dtResult.Rows[0]["SubscribeReject"].ToString();
                }
                if (_dtResult.Rows[0]["Reject"] != DBNull.Value && _dtResult.Rows[0]["Reject"].ToString() != string.Empty)
                {
                    Reject = _dtResult.Rows[0]["Reject"].ToString();
                }
                if (_dtResult.Rows[0]["Users"] != DBNull.Value && _dtResult.Rows[0]["Users"].ToString() != string.Empty)
                {
                    Users = _dtResult.Rows[0]["Users"].ToString();
                }
                if (Error.ToString().ToUpper() != "ERROR")
                {
                    lblMessage.Text = "Successfully Unsubscribed. (" + SubscribeReject + ") from Subsriber Table, ("
                        + Users + ") from User Table and (" + Reject + ") from others";
                    lblError.Text = "";
                    if (txtAlredyRejected.Text.Trim().Length > 5)
                    {
                        trAlredyRejected.Visible = true;
                    }
                }
                else
                {
                    lblMessage.Text = "";
                    lblError.Text = "Unsubscribed Faild. Try Again later!";
                }
            }
            else
            {
                lblMessage.Text = "";
                lblError.Text = "Unsubscribed Faild. Try Again later!";
            }
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearFields();
    }

    private void ClearFields()
    {
        trAlredyRejected.Visible = false;
        ddlRejectType.SelectedIndex = 0;
        txtEmail.Text = "";
        txtAlredyRejected.Text = "";
        lblMessage.Text = "";
        lblError.Text = "";
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnReject.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnReject.Enabled = true;
        }
    }

}
